<!DOCTYPE html>
<html>
<head>
	<title>List of Deposits</title>
</head>
<body>
	<?php
		
		// include of required files
		require_once ("constants.php");
		require_once ("planets_class.php");
		require_once ("deposits_class.php");
		require_once ("functions.php");
		
		// set the working view to "list"
		$view = "view";
		
		// input parameters from post
		$planet_id = $_POST['planet_id'];
		if(isset($_POST['db_action'])) {$db_action = $_POST['db_action'];}
		if(isset($_POST['dep_id'])) {
			$dep_id = $_POST['dep_id'];
			//if deposit id is set, creates a new deposit instance
			$deposit = new deposits($dep_id);
		}
		if(isset($_POST['dep_size'])) {$dep_size = $_POST['dep_size'];}
		if(isset($_POST['x_coord'])) {$x_coord = $_POST['x_coord'];}
		if(isset($_POST['y_coord'])) {$y_coord = $_POST['y_coord'];}
		if(isset($_POST['material'])) {$material = $_POST['material'];}
		
		//create a new planet instance
		$planet = new planets($planet_id);
		
		// retrieves planet data from the database
		$planet_data = $planet->ReadPlanet();

		// html header
		echo "<h1 align=\"center\">Planet Name: ".$planet_data['name']."</h1>\n";
		echo "<h2 align=\"center\">List of Deposits</h2><br />\n";
		
		// check the db_action (insert, delete, update) and call the appropiate function
		if(isset($db_action)){
			
			if($db_action === "delete"){
				
				// if the action is "delete", deletes the deposit $dep_id
				if($delete_error = $deposit->DeleteDeposit()){
					
					echo "<p align=\"center\"><strong>Error deleting deposit: " .$delete_error."</strong></p><br>";
					
				} else {
					
					echo "<p align=\"center\"><strong>Deposit deleted</strong></p>";

				} 
			}
			
			
			if($db_action === "insert"){
				
				// if the action is "insert", insert a new deposit.	
				if(depositIsValid($material, $dep_size, $x_coord, $y_coord, $planet_data['size']))	{
								
					if($insert_error = deposits::InsertDeposit($planet_id, $x_coord, $y_coord, $material, $dep_size, DB_USER)){
						
						echo "<p align=\"center\"><strong>Error creating new deposit: " .$insert_error."</strong></p>";	
					} 
					else {
						
						echo "<p align=\"center\"><strong>New deposit created in (".$x_coord.", ".$y_coord."): " . $dep_size . " units of " . $material."</strong></p>";
					}	
				}							
			}
			
			if($db_action === "update"){
				
				// if the action is "update", update the deposit.	
				if(depositIsValid($material, $dep_size))	{
					if($update_error = $deposit->UpdateDeposit($material, $dep_size, DB_USER)){
						
						echo "<p align=\"center\"><strong>Error updating new deposit: " .$update_error."</strong></p>";
						
					} else {
						
						echo "<p align=\"center\"><strong>Deposit updated: " . $dep_size . " units of " . $material."</strong></p>";
					}
				}
			}
				
		}

		// creates a "switch to map view" button
		echo "<p><FORM METHOD=\"post\" ACTION=\"mapDepos.php\" style=\"margin-left: auto; margin-right: auto; text-align: center;\">\n";
		echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">\n";
		echo "<input type=\"submit\" value=\"Map View\" name=\"editar\">\n";
		echo "</FORM></p><br />\n";
		
		// retrieves an array with all the deposits of the planet				
		$data = $planet->AllDeposits();

		// if the planet has deposits, display a table of deposits
		if($data){

			echo "<table width=\"75%\" border=1 RULES=ALL FRAME=BOX align=\"center\" cellspacing=\"0\" cellpadding=\"0\">\n";
			echo "<tr align=\"center\" valign=\"center\"><th>Position</th><th>Material</th><th>Size</th><th>User</th><th>Last Modified</th><th colspan=\"2\">Actions</th></tr>\n";
			
			foreach($data as $depos){
				echo "<tr align=\"center\" valign=\"center\">\n";

				echo "<td>(".$depos['x_coord'].", ".$depos['y_coord'].")</td><td>".$depos['material']."</td><td>".$depos['size']."</td><td>".$depos['user']."</td><td>".$depos['time_mod']."</td>";

				// creates a "delete" and "edit" button for each of the deposits
				echo "<td><p><FORM METHOD=\"post\" ACTION=\"deposit.php\">";
				echo "<input type=\"hidden\" name=\"dep_id\" value=".$depos['id'].">";
				echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">";
				echo "<input type=\"hidden\" name=\"planet_name\" value=\"".$planet_data['name']."\">";
				echo "<input type=\"hidden\" name=\"planet_size\" value=".$planet_data['size'].">";
				echo "<input type=\"hidden\" name=\"planet_view\" value=\"list\">";
				echo "<input type=\"submit\" value=\"Edit\" name=\"editar\">";
				echo "</FORM></p></td>";
				
				echo "<td><p><FORM METHOD=\"post\" ACTION=\"listDepos.php\">";
				echo "<input type=\"hidden\" name=\"db_action\" value=\"delete\">\n";
				echo "<input type=\"hidden\" name=\"dep_id\" value=".$depos['id'].">";
				echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">";
				echo "<input type=\"submit\" value=\"Delete\" name=\"editar\">";
				echo "</FORM></p></td>";
				
				echo "</tr>\n";
			}
			echo "</table><br>\n";

		}
		
		// creates an "add deposit" button
		echo "<p><FORM METHOD=\"post\" ACTION=\"AddDeposit.php\" style=\"margin-left: auto; margin-right: auto; text-align: center;\">";
		echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">";
		echo "<input type=\"hidden\" name=\"planet_name\" value=\"".$planet_data['name']."\">";
		echo "<input type=\"hidden\" name=\"planet_size\" value=".$planet_data['size'].">";
		echo "<input type=\"hidden\" name=\"planet_view\" value=".$view.">";
		echo "<input type=\"submit\" value=\"Add Deposit\" name=\"editar\">";
		echo "</FORM></p>";

	?>

	<br>
	<p align="center"><a href="index.php">Back to List of Planets</a></p>
</body>
</html>
